from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.status import HTTP_404_NOT_FOUND, HTTP_204_NO_CONTENT
from rest_framework.viewsets import ModelViewSet

from .models import Student, Classes
from .serializers import StudentSerializer, ClassesSerializer


# Create your views here.
# 使用ModelViewSet编写增删改查
class StudentViewSet(ModelViewSet):
    queryset = Student.objects.filter(is_delete=False, classes__is_delete=False)
    # 只获取没有进行逻辑删除的数据, classes__is_delete=False:删除班级后，学生也不展示
    # queryset = Student.objects.all()  # queryset：数据集
    serializer_class = StudentSerializer  # serializer_class：定义序列化器是哪个


class ClassesViewSet(ModelViewSet):
    queryset = Classes.objects.filter(is_delete=False)  # 只获取没有进行逻辑删除的数据
    serializer_class = ClassesSerializer  # serializer_class：定义序列化器是哪个

    # 查询最新建立的班级或学生
    @action(methods=['get'], detail=False)  # action装饰器自动生成路由，detail=False表示不需要获得详细的数据
    def last(self, request):
        classes = Classes.objects.filter(is_delete=False).last()  # 获取没有逻辑删除的班级中最新建立的班级
        serializer = self.get_serializer(classes)  # 获得实例对象的序列化器，并经classes序列化
        return Response(serializer.data)

# # 使用ViewSet编写增删改查
# class StudentViewSet(viewsets.ViewSet):
#     # 查找，对应请求get
#     def list(self, request):
#         # 获取所有学生的信息，返回的是模型类集合
#         students = Student.objects.all()
#         # 序列化，实例化序列化器，传入要转换的模型集合，如果
#         # 是多条数据，一般加many=True
#         serializer = StudentSerializer(students, many=True)
#         # 返回响应，返回序列化后的数据
#         return Response(data=serializer.data)
#
#     # 创建，对应请求post
#     def create(self, request):
#         # 获取前的传入的数据
#         serializer = StudentSerializer(data=request.data)  # 获取前端传来的数据，转成模型，反序列化
#         # 校验
#         serializer.is_valid(raise_exception=True)  # 如果校验不通过，会抛出异常
#         # 保存数据
#         serializer.save()
#         # 返回响应
#         return Response(serializer.data)
#
#     # 单一查找，对应请求get
#     def retrieve(self, request, pk):
#         # 获取数据
#         try:
#             student = Student.objects.get(id=pk)  # 获取id为pk的学生数据
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)  # 如果数据不存在返回404
#         # 序列化获得的数据
#         serializer = StudentSerializer(instance=student)  # 传instance进行序列化，传data进行反序列化
#         # 返回数据
#         return Response(serializer.data)
#
#     # 更新数据
#     def update(self, request, pk):
#         # 获取需要修改的数据
#         try:
#             student = Student.objects.get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         # 将从前的获取的数据进行反序列化
#         serializer = StudentSerializer(instance=student, data=request.data)
#         # 序列化对象没有传入instance，save()方法做create处理，否则进行update处理
#         # 校验数据
#         serializer.is_valid(raise_exception=True)
#         # 对数据进行修改
#         serializer.save()
#         return Response(serializer.data)
#         # 通过序列化器对象的date属性获得序列化后的数据（json格式）
#
#     # 删除数据
#     def destroy(self, request, pk):
#         # 获取需要修改的数据
#         try:
#             student = Student.objects.get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         # 删除数据
#         student.delete()
#         return Response(status=HTTP_204_NO_CONTENT)
